import { OAuth2Client, TokenPayload } from 'google-auth-library';
import { publicKeysString } from '../assets/google-publickeys';
import jwt from 'jsonwebtoken';
import { app } from '..';
import { Prisma, User, UserToken } from '@prisma/client';
import { z } from 'zod';
import { SECRET } from '../config/env';

class UserTokenService {
    generate<T extends object>(data: T) {
        const createAt = +new Date();
        return jwt.sign({...data, createAt}, SECRET, { expiresIn: '30d' });
    }

    async create(user: User) {
        const userToken = this.generate({ email: user.email });
        const token = await app.prisma.userToken.create({ data: { userId: user.id, token: userToken } });
        return token;
    }

    async get(user: User) {
        return await app.prisma.userToken.findMany({ where: { userId: user.id } });
    }

    async findById(id: number) {
        return await app.prisma.userToken.findUnique({ where: { id } });
    }

    async query(user: Partial<UserToken>) {
        return await app.prisma.userToken.findMany({ where: user });
    }
}

export default new UserTokenService();